<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="属于的章节id" prop="chapterId">
        <el-input
          v-model="queryParams.chapterId"
          placeholder="请输入属于的章节id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目答案" prop="answer">
        <el-input
          v-model="queryParams.answer"
          placeholder="请输入题目答案"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目分值" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入题目分值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序顺序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序顺序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入创建人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目中图片" prop="questionImg">
        <el-input
          v-model="queryParams.questionImg"
          placeholder="请输入题目中图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:exercise:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:exercise:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:exercise:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:exercise:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="exerciseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="习题id" align="center" prop="exerciseId" />
      <el-table-column label="属于的章节id" align="center" prop="chapterId" />
      <el-table-column
        label="习题题目描述"
        align="center"
        prop="questionName"
      />
      <el-table-column label="习题类型" align="center" prop="questionType" />
      <el-table-column label="题目选项" align="center" prop="options" />
      <el-table-column label="题目答案" align="center" prop="answer" />
      <el-table-column label="答案解析" align="center" prop="analysis" />
      <el-table-column label="题目分值" align="center" prop="score" />
      <el-table-column label="排序顺序" align="center" prop="sort" />
      <el-table-column label="创建人id" align="center" prop="userId" />
      <el-table-column label="题目中图片" align="center" prop="questionImg" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bank_status" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:exercise:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:exercise:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="属于的章节id" prop="chapterId">
          <el-input v-model="form.chapterId" placeholder="请输入属于的章节id" />
        </el-form-item>
        <el-form-item label="习题题目描述" prop="questionName">
          <el-input
            v-model="form.questionName"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="习题类型" prop="questionType">
          <el-select v-model="form.questionType" placeholder="请选择习题类型">
            <el-option
              v-for="dict in dict.type.question_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目选项" prop="options">
          <el-input
            v-model="form.options"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="题目答案" prop="answer">
          <el-input v-model="form.answer" placeholder="请输入题目答案" />
        </el-form-item>
        <el-form-item label="答案解析" prop="analysis">
          <el-input
            v-model="form.analysis"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="题目分值" prop="score">
          <el-input v-model="form.score" placeholder="请输入题目分值" />
        </el-form-item>
        <el-form-item label="排序顺序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序顺序" />
        </el-form-item>
        <el-form-item label="创建人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入创建人id" />
        </el-form-item>
        <el-form-item label="题目中图片" prop="questionImg">
          <el-input v-model="form.questionImg" placeholder="请输入题目中图片" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-divider content-position="center">知识选项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddWxOptionValue"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteWxOptionValue"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="wxOptionValueList"
          :row-class-name="rowWxOptionValueIndex"
          @selection-change="handleWxOptionValueSelectionChange"
          ref="wxOptionValue"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="习题标签" prop="optionLable" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.optionLable"
                placeholder="请输入习题标签"
              />
            </template>
          </el-table-column>
          <el-table-column label="习题文案" prop="optionValue" width="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.optionValue"
                placeholder="请输入习题文案"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listExercise,
  getExercise,
  delExercise,
  addExercise,
  updateExercise,
} from "@/api/program/exercise";

export default {
  name: "Exercise",
  dicts: ["question_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWxOptionValue: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识库表格数据
      exerciseList: [],
      // 知识选项表格数据
      wxOptionValueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chapterId: null,
        questionName: null,
        questionType: null,
        options: null,
        answer: null,
        analysis: null,
        score: null,
        sort: null,
        userId: null,
        questionImg: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chapterId: [
          { required: true, message: "属于的章节id不能为空", trigger: "blur" },
        ],
        questionName: [
          { required: true, message: "习题题目描述不能为空", trigger: "blur" },
        ],
        questionType: [
          { required: true, message: "习题类型不能为空", trigger: "change" },
        ],
        answer: [
          { required: true, message: "题目答案不能为空", trigger: "blur" },
        ],
        analysis: [
          { required: true, message: "答案解析不能为空", trigger: "blur" },
        ],
        score: [
          { required: true, message: "题目分值不能为空", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "排序顺序不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "更新人不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "创建人id不能为空", trigger: "blur" },
        ],
        state: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询知识库列表 */
    getList() {
      this.loading = true;
      listExercise(this.queryParams).then((response) => {
        this.exerciseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        exerciseId: null,
        chapterId: null,
        questionName: null,
        questionType: null,
        options: null,
        answer: null,
        analysis: null,
        score: null,
        sort: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        userId: null,
        questionImg: null,
        state: null,
      };
      this.wxOptionValueList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.exerciseId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const exerciseId = row.exerciseId || this.ids;
      getExercise(exerciseId).then((response) => {
        this.form = response.data;
        this.wxOptionValueList = response.data.wxOptionValueList;
        this.open = true;
        this.title = "修改知识库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.wxOptionValueList = this.wxOptionValueList;
          if (this.form.exerciseId != null) {
            updateExercise(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExercise(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const exerciseIds = row.exerciseId || this.ids;
      this.$modal
        .confirm('是否确认删除知识库编号为"' + exerciseIds + '"的数据项？')
        .then(function () {
          return delExercise(exerciseIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 知识选项序号 */
    rowWxOptionValueIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 知识选项添加按钮操作 */
    handleAddWxOptionValue() {
      let obj = {};
      obj.optionLable = "";
      obj.optionValue = "";
      this.wxOptionValueList.push(obj);
    },
    /** 知识选项删除按钮操作 */
    handleDeleteWxOptionValue() {
      if (this.checkedWxOptionValue.length == 0) {
        this.$modal.msgError("请先选择要删除的知识选项数据");
      } else {
        const wxOptionValueList = this.wxOptionValueList;
        const checkedWxOptionValue = this.checkedWxOptionValue;
        this.wxOptionValueList = wxOptionValueList.filter(function (item) {
          return checkedWxOptionValue.indexOf(item.index) == -1;
        });
      }
    },
    /** 复选框选中数据 */
    handleWxOptionValueSelectionChange(selection) {
      this.checkedWxOptionValue = selection.map((item) => item.index);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/exercise/export",
        {
          ...this.queryParams,
        },
        `exercise_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
